package com.qtenv.datasync.service.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.qtenv.datasync.base.Result;
import com.qtenv.datasync.service.entity.BalanceFix;
import com.qtenv.datasync.service.service.BalanceFixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 分区水平衡修正 前端控制器
 * </p>
 *
 * @author CBL
 * @since 2022-08-12
 */
@RestController
@RequestMapping("/balance-fix")
public class BalanceFixController {
    @Autowired
    private BalanceFixService balanceFixService;

    @GetMapping("/getZoneBalanceFix")
    public Result<?> getZoneBalanceFix(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId) {
        return Result.ok(this.transformZoneBalanceFixList(balanceFixService.getZoneBalanceFix(tenantId, areaId)));
    }

    private List<Object> transformZoneBalanceFixList(BalanceFix balanceFix) {
        System.out.println(JSONUtil.toJsonPrettyStr(balanceFix));
        List<Object> result = CollectionUtil.newArrayList();

        String[] keyArr = {"selfflow", "buyflow", "totalflow", "userflow", "totalleakage2", "chargeflowJl,chargeflowWjl", "freeflowJl,freeflowWjl",
                "totalleakage1", "cleakage", "wleakage", "chargeflowJl", "chargeflowWjl", "freeflowJl", "freeflowWjl", "mleakage", "aleakage",
                "backleakage", "sleakage", "jleakage", "fleakage", "wleakage"
        };

        if (balanceFix != null) {
            for (String key : keyArr) {
                if (key.indexOf(",") == -1) {
                    result.add(BeanUtil.getFieldValue(balanceFix, key));
                } else {
                    System.out.println(key);
                    String[] subKeyArr = key.split(",");
                    result.add(Double.valueOf(BeanUtil.getFieldValue(balanceFix, subKeyArr[0]).toString()) + Double.valueOf(BeanUtil.getFieldValue(balanceFix, subKeyArr[1]).toString()));
                }
            }
        }

        return result;
    }
}
